/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.rotaryTableRecord.service.impl;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.rotaryTableRecord.domain.RotaryTableRecord;
import co.yixiang.modules.rotaryTableRecord.service.RotaryTableRecordService;
import co.yixiang.modules.rotaryTableRecord.service.dto.RotaryTableRecordDto;
import co.yixiang.modules.rotaryTableRecord.service.dto.RotaryTableRecordQueryCriteria;
import co.yixiang.modules.rotaryTableRecord.service.mapper.RotaryTableRecordMapper;
import co.yixiang.modules.user.domain.YxUser;
import co.yixiang.modules.user.domain.YxUserAddress;
import co.yixiang.modules.user.service.YxUserAddressService;
import co.yixiang.modules.user.service.YxUserService;
import co.yixiang.utils.FileUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author hupeng
* @date 2020-12-23
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "rotaryTableRecord")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class RotaryTableRecordServiceImpl extends BaseServiceImpl<RotaryTableRecordMapper, RotaryTableRecord> implements RotaryTableRecordService {

    private final IGenerator generator;
    @Autowired
    private  YxUserService yxUserService;
    @Autowired
    private YxUserAddressService yxUserAddressService;


    @Override
    //@Cacheable
    public Map<String, Object> queryAll(RotaryTableRecordQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<RotaryTableRecord> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);

        List<RotaryTableRecordDto> rotaryTableRecordDtos = generator.convert(page.getList(), RotaryTableRecordDto.class);
        for(RotaryTableRecordDto rotaryTableRecordDto :rotaryTableRecordDtos){
            if(ObjectUtil.isNotEmpty(rotaryTableRecordDto.getUserAddress())){
                YxUserAddress yxUserAddress =  yxUserAddressService.getById(rotaryTableRecordDto.getUserAddress());
                if(ObjectUtil.isNotEmpty(yxUserAddress)){
                    rotaryTableRecordDto.setUserAddress(yxUserAddress.getDetail());
                }
            }
        }
        map.put("content", rotaryTableRecordDtos);
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    public List<RotaryTableRecord> queryAll(RotaryTableRecordQueryCriteria criteria){
        List<RotaryTableRecord> list = this.baseMapper.selectRotaryList(criteria);
        return list;
    }

    @Override
    //@Cacheable
    public Page<RotaryTableRecord> queryAllPage(Page page,Long uid){
        return baseMapper.rotaryPage(page,uid);
    }


    @Override
    public void download(List<RotaryTableRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RotaryTableRecordDto rotaryTableRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户名", rotaryTableRecord.getUserName());
            map.put("手机号", rotaryTableRecord.getPhone());
            map.put("奖品名称", rotaryTableRecord.getName());
            if(ObjectUtil.isNotEmpty(rotaryTableRecord.getUserAddress())){
                YxUserAddress yxUserAddress =  yxUserAddressService.getById(rotaryTableRecord.getUserAddress());
                if(ObjectUtil.isNotEmpty(yxUserAddress)){
                    map.put("详细地址", yxUserAddress.getDetail());
                }
            }

            if(rotaryTableRecord.getType() == 0){
                map.put("商品类型", "真实商品");
            }else {
                map.put("商品类型", "虚拟商品");
            }
            if(rotaryTableRecord.getStatus() == 0){
                map.put("状态", "未处理");
            }else {
                map.put("状态", "已处理");
            }
            map.put("抽奖时间", rotaryTableRecord.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


}
